package com.hu.hrm.config;

import com.hu.hrm.constants.MQConstants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//mq的配置
@Configuration
public class RabbitMQConfig {

    /*
    * 配置文件中配置routingKey
    * */
    //读取routingkey
    @Value("${site.routingkey}")
    private String routingKey;

    //交换机
    @Bean(MQConstants.NAME_EXCHANGE_PAGE_DIRECT)
    public Exchange exchange() {
        return ExchangeBuilder.topicExchange(MQConstants.NAME_EXCHANGE_PAGE_DIRECT).durable(true).build();
    }

    //声明队列
    @Bean(MQConstants.NAME_QUEUE_PAGE_DIRECT)
    public Queue queue() {
        Queue queue = new Queue(MQConstants.NAME_QUEUE_PAGE_DIRECT,true);
        return queue;
    }

    //routingKey 一般不写死！  因为微服务的多样性 routingKey不同
    //这里用配置文件配置routingKey的方式
    @Bean
    public Binding bindingQueueInformEmail() {
        return BindingBuilder.bind(queue()).to(exchange()).with(routingKey).noargs();
    }

}
